package com.example.baserecyclerviewadapterdemo.adapter;

import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.baserecyclerviewadapterdemo.R;
import com.example.baserecyclerviewadapterdemo.util.ClickableMovementMethod;
import com.example.baserecyclerviewadapterdemo.util.SpannableStringUtils;
import com.example.baserecyclerviewadapterdemo.entity.Status;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class AnimationAdapter extends BaseQuickAdapter<Status, BaseViewHolder> {
    public AnimationAdapter(int layoutResId, @Nullable List<Status> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, Status status) {
        switch (holder.getLayoutPosition() % 3) {
            case 0:
                holder.setImageResource(R.id.img,R.mipmap.animation_img1);
                break;
            case 1:
                holder.setImageResource(R.id.img,R.mipmap.animation_img2);
                break;
            case 2:
                holder.setImageResource(R.id.img,R.mipmap.animation_img3);
                break;
        }
        holder.setText(R.id.tweetName,"Hoteis in Rio de Janeiro");
        String msg = "\"He was one of Australia's most of distinguished artistes, renowned for his portraits\"";
        ((TextView)holder.getView(R.id.tweetText)).setText(SpannableStringUtils.getBuilder(msg)
                .append("landscapes and nedes").setClickSpan(clickableSpan).create());
        ((TextView) holder.getView(R.id.tweetText)).setMovementMethod(ClickableMovementMethod.getInstance());
        holder.getView(R.id.tweetText).setFocusable(false);
        holder.getView(R.id.tweetText).setClickable(false);
        holder.getView(R.id.tweetText).setLongClickable(false);
    }

    private ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(@NonNull View widget) {
            Toast.makeText(getContext(), "事件触发了 landscapes and needs", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void updateDrawState(@NonNull TextPaint ds) {
            ds.setColor(getContext().getColor(R.color.clickspan_color));
            ds.setUnderlineText(true);
        }
    };
}
